package com.atguigu.cloud.config.securityConfig.Request;/**
 * @Auter 李孝在
 * @Date 2024/11/18
 */

/**
 * @projectName: keshe
 * @package: com.atguigu.cloud.config.securityConfig.Request
 * @className: asd
 * @author: Eric
 * @description: TODO
 * @date: 2024/11/18 11:38
 * @version: 1.0
 */

import com.alibaba.fastjson2.JSON;
import com.atguigu.cloud.tool.resp.ResultData;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.security.core.Authentication;
import org.springframework.security.web.authentication.AuthenticationSuccessHandler;

import javax.servlet.ServletException;
import java.io.IOException;

public class BaseAuthenticationSuccessHandler implements AuthenticationSuccessHandler {
    @Override
    public void onAuthenticationSuccess(HttpServletRequest request, HttpServletResponse response, Authentication authentication) throws IOException {

        //获取用户身份信息
        Object principal = authentication.getPrincipal();

        ResultData<Object> result=ResultData.success(principal);

        //转换成json字符串
        String json = JSON.toJSONString(result);
        //返回响应
        response.setContentType("application/json;charset=UTF-8");
        response.getWriter().println(json);
    }
}